package com.yangtao.springbootfreemarker.domain.sys.dao;

import com.yangtao.core.jpa.dao.BaseJpaRepository;
import com.yangtao.springbootfreemarker.domain.sys.entity.SysUser;
import org.hibernate.annotations.SQLInsert;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Repository
public interface SysUserDao extends BaseJpaRepository<SysUser, String> {

	/*@Query(value = "select id, old_id, name from single_project sp where sp.old_id is not null", nativeQuery = true)
	List<Map<String, Object>> getSingProjectList();

	@Query(value = "select id, old_id, name, file as file, file_url as fileUrl  from record r where r.single_project = ?1 and r.old_id is not null", nativeQuery = true)
	List<Map<String, Object>> getRecordList(String id);

	@Query(value = "update record t set t.file_url = ?2 where t.id = ?1", nativeQuery = true)
	@Modifying
	@Transactional
	void updateRecordFileUrl(String id, String oldPath);

	@Query(value = "select id, old_id, name from single_project sp", nativeQuery = true)
	List<Map<String, Object>> getAllSingProjectList();

	@Query(value = "select t.id as id, t.name as name, t.file_url as fileUrl, t.record_sliceId as sliceId, t.old_id as oldId  from record t left join accessories_info ai on ai.aii10c = t.id where t.file_url is not null and ai.aii01c is  null and t.single_project = ?1", nativeQuery = true)
	List<Map<String, Object>> getRecordNoAIList(String singProjectId);

	@Query(value = "insert into accessories_info values (?1, ?2, 1, ?3, ?4, 0, '2020-05-14 17:32:52', 'admin', 'record', ?5, 0, 1, 'admin', '628000', 0, null, null)" , nativeQuery = true)
	@Modifying
	@Transactional
	void insertAccessoriesInfo(String id, String name,long fileSize, String filePath, String dataId);

	@Query(value = "select t.id as id, t.name as name, t.file_url as fileUrl, t.record_sliceId as sliceId, t.old_id as oldId, ai.aii05c as accFileUrl  from record t left join accessories_info ai on ai.aii10c = t.id where t.file_url is null and ai.aii01c is not null and t.single_project = ?1", nativeQuery = true)
	List<Map<String, Object>> getRecordYesAIList(String singProjectId);*/

	/**
	 * 获取当前用户的权限列表数据
	 * @param userId
	 * @return
	 */
	@Query(value = "select DISTINCT sm.perms from sys_user_role sur " +
			"left join sys_role_menu srm on srm.role_id = sur.role_id " +
			"LEFT JOIN sys_menu sm on sm.menu_id = srm.menu_id " +
			"where sur.user_id = ?1 and sm.perms is not null" , nativeQuery = true)
	List<String> queryAllPerms(String userId);
}
